//
//  NSObject+SHMethodSwizzle.m
//  SDemo
//
//  Created by gg on 2018/7/17.
//  Copyright © 2018年 gg. All rights reserved.
//

#import "NSObject+SHMethodSwizzle.h"
#import <objc/message.h>

@implementation NSObject (SHMethodSwizzle)

+ (void)intanceMethodExchangeWithOriginSelector:(SEL)sel1 swizzledSelector:(SEL)sel2{
    Class class = [self class];
    
    if (![class instanceMethodForSelector:sel1] || ![class instanceMethodForSelector:sel2] ) {
        return;
    }
    
    
    Method method1 = class_getInstanceMethod(class, sel1);
    Method method2 = class_getInstanceMethod(class, sel2);
    
    BOOL didAddMethod =  class_addMethod(class, sel1, method_getImplementation(method2), method_getTypeEncoding(method2));
    
    if (didAddMethod) {
        class_replaceMethod(class, sel2,method_getImplementation(method1) , method_getTypeEncoding(method1));
        
    } else {
        method_exchangeImplementations(method1, method2);
    }
}

@end
